package com.cosina.game.crystallight.ui.battle.layer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;

import com.cosina.game.crystallight.engine.BattleEngine;
import com.cosina.game.crystallight.model.battle.Ability;
import com.cosina.game.crystallight.model.battle.Tile;
import com.cosina.game.crystallight.ui.battle.BattleResource;
import com.cosina.game.crystallight.ui.battle.drawadapter.BaseLayer;
import com.cosina.game.crystallight.ui.common.PaintSuite;
import com.cosina.game.crystallight.util.World;

public class BackgroundLayer extends BaseLayer{
	
	final static int castleYSpan = -1, castleXSpan = 30;
	final static int startX = 5, startY = 5;
	
	private void drawCannotBoxAndMixture(Canvas canvas) {
		
		Bitmap bitmap = BattleResource.get(-Ability.GENM);

		for (int i = 0; i < 3; i++) {
			canvas.drawBitmap(bitmap, 44 * i + 1, 5+44*9,
					PaintSuite.p4Simple);
		}

		canvas.drawBitmap(BattleResource.get(Ability.MIXTURE),  44 * 3 + 1,
				5+44*9, PaintSuite.p4Simple);

		for (int i = 0; i < 3; i++) {
			canvas.drawBitmap(bitmap, 44 * (i + 3) + 54 + 1, 5+44*9,
					PaintSuite.p4Simple);
		}
	}
	
	private static void drawBorder(Canvas canvas){
		canvas.drawBitmap(BattleResource.ram1, 0, 0, PaintSuite.p4Simple);
		canvas.drawBitmap(BattleResource.ram2, 0, 5, PaintSuite.p4Simple);
		canvas.drawBitmap(BattleResource.ram3, 320-5, 5, PaintSuite.p4Simple);
		canvas.drawBitmap(BattleResource.ram1, 0, 475, PaintSuite.p4Simple);
	}
	
	private static void drawManabarBorder(Canvas canvas){
		canvas.drawBitmap(BattleResource.manabarBorder, 0, 480-5-30, PaintSuite.p4Simple);
	}
	
	private static void drawGround(Canvas canvas){
		int[][] tiles = BattleEngine.field.groudData();
		for(int y=0; y<tiles.length; y++) {
			for(int x = 0; x <tiles[0].length; x++){
				int tileStyle = tiles[y][x];
				if(tileStyle >= 0){
					{
						int groundPart = tileStyle%10;
						canvas.drawBitmap(BattleResource.getTile(BattleEngine.field.getMapType(),groundPart + 8), x*44 + startX, y*44 + startY, PaintSuite.p4Simple);
					}
				}else if(tileStyle < 0){
					boolean leftIsRoad = x == 0 ? false :tiles[y][x-1] < 0;
					boolean topIsRoad = y==0 ? false :tiles[y-1][x] < 0;
					boolean rightIsRoad = x == 6 ? false : tiles[y][x+1] < 0;
					boolean downIsRoad = y==8 ? false :tiles[y+1][x] < 0;
					int roadType = Tile.getTileType(leftIsRoad, topIsRoad, rightIsRoad, downIsRoad);
					if(-1 == roadType){
						continue;
					}
					Bitmap bitmap = BattleResource.getTile(BattleEngine.field.getMapType(),roadType);
					canvas.drawBitmap(bitmap, x*44 + startX, y*44 + startY, PaintSuite.p4Simple);
				}
			}
		}
	}
	
	private static void drawCastleHome(Canvas canvas){
		Point castleLocation = BattleEngine.field.castleLocation();
		Bitmap castleBitmap = BattleEngine.field.castleDirection() == World.LEFT ? BattleResource.castleV : BattleResource.castleH;
		if(BattleEngine.field.castleDirection() == World.LEFT){
			canvas.drawBitmap(castleBitmap, castleLocation.x * 44 + castleXSpan + startX, castleLocation.y * 44 + castleYSpan + startY, PaintSuite.p4Simple);
		}else{
			canvas.drawBitmap(castleBitmap, castleLocation.x * 44 + castleYSpan + startX, castleLocation.y * 44 + castleXSpan + startY, PaintSuite.p4Simple);
		}
	}
	
	private static void drawMSShield(Canvas canvas) {
		Point location = BattleEngine.field.castleLocation();
		int	shieldX = location.x + 1;
		int	shieldY = location.y + 1;
		canvas.drawBitmap(BattleResource.msShield, shieldX * 44 + 8, shieldY *44 + 8, PaintSuite.p4Simple);
	}
	
	protected void draw(Canvas canvas) {
		drawBorder(canvas);
		drawManabarBorder(canvas);
		drawGround(canvas);
		drawOverlay(canvas);
		drawCastleHome(canvas);
		drawMSShield(canvas);
		drawCannotBoxAndMixture(canvas);
	}

	private void drawOverlay(Canvas canvas) {
		int[][] tiles = BattleEngine.field.groudData();
		for(int y=0; y<tiles.length; y++) {
			for(int x = 0; x <tiles[0].length; x++){
				int tileStyle = tiles[y][x];
				if(tileStyle >= 0){
					{
						int overlayPart = tileStyle%1000/10;
						if(overlayPart != 0){
							canvas.drawBitmap(BattleResource.overlay[overlayPart], x*44 + startX, y*44 + startY, PaintSuite.p4Simple);
						}
					}
				}
			}
		}
	}
}
